package com.grgbanking.it.system.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grgbanking.it.common.entity.PageQuery;
import com.grgbanking.it.common.json.JSONHelper;
import com.grgbanking.it.common.util.DateUtil;
import com.grgbanking.it.system.entity.Role;
import com.grgbanking.it.system.entity.User;
import com.grgbanking.it.system.service.IRoleService;

/**
 * 角色管理
 * 
 * RoleController
 * 
 * @author 潘广伟
 * @Email p_3er@qq.com
 * @Date 2014-12-18 上午9:37:21
 * 
 * @version 1.0.0
 *
 */
@Controller
@RequestMapping("/system")
public class RoleController {
	
	@Autowired
	private IRoleService roleService;
	
	@ResponseBody  
	@RequestMapping(value="role",method=RequestMethod.POST)
    public Object save(HttpSession httpSession,Role role){  
		String json = null;
        try {
        	json = saveOrUpdate(httpSession, role);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
        return json;  
    }   
	
	@ResponseBody  
	@RequestMapping(value="role/{id}",method=RequestMethod.PUT)
	public Object update(HttpSession httpSession,@PathVariable("id") Integer id,Role role){  
		String json = null;
		try {
			json = saveOrUpdate(httpSession, role);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}
	
	/**
	 * 保存或更新
	 * saveOrUpdate
	 * 
	 * @param httpSession
	 * @param role
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	private String saveOrUpdate(HttpSession httpSession, Role role)
			throws Exception {
		User user = (User) httpSession.getAttribute("loginUser");
		role.setCreator(user.getUserId());
		role.setCreateTime(DateUtil.getInstance().todayShort());
		roleService.saveOrUpdate(role);
		return JSONHelper.getSuccessMessage("操作成功");
	}   
	
	/**
	 * 查询所有
	 * search
	 * 
	 * @param httpSession
	 * @param key
	 * @param query
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="role",method=RequestMethod.GET)
	public Object search(HttpSession httpSession,String username,String key,PageQuery query){  
		String json = null;
		try {
			json = roleService.findByPageToJson(query,username,key);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}   
	
	/**
	 * 获取单个
	 * @param operationWork
	 * @param start
	 * @param limit
	 * @return
	 */
	@ResponseBody  
	@RequestMapping(value="role/{id}",method=RequestMethod.GET)
	public Object get(@PathVariable("id") Integer id){  
		String json = null;
		try {
			json = roleService.findByIdToJson(id);
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		return json;  
	}  
	
	/**
	 * 删除
	 * delete
	 * 
	 * @param id
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="role/{id}",method=RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Integer id){  
		String json = null;
		try {
			roleService.delete(id);
			json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
		
		return json;  
	}  
	
	/**
	 * 保存或更新角色资源
	 * saveOrUpdateMRoleResource
	 * 
	 * @param httpSession
	 * @param submitJson json格式的数据
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody  
	@RequestMapping(value="role/resources",method=RequestMethod.POST)
    public Object distributeResources(Integer id,String resourceIds){  
		String json = null;
        try {
        	roleService.updateDistributeResources(id, resourceIds);
        	json = JSONHelper.getSuccessMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			json = JSONHelper.getExceptionMessage(e);
		}  
        return json;  
    }   
}
